home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / comms / internet / html-related / hsc / source / hsclib / include.c < prev    next >
C/C++ Source or Header  |  1996-09-06  |  7KB  |  245 lines

  1. /*
  2.  * hsclib/include.c
  3.  *
  4.  * hsc include functions
  5.  *
  6.  * Copyright (C) 1995,96  Thomas Aglassinger
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * updated:  6-Sep-1996
  23.  * created: 19-Feb-1996
  24.  */
  25.  
  26. #include <time.h>
  27.  
  28. #include "hsclib/inc_base.h"
  29.  
  30. #include "hsclib/input.h"
  31. #include "hsclib/parse.h"
  32.  
  33. #define NOEXTERN_HSCLIB_INCLUDE_H
  34. #include "hsclib/include.h"
  35.  
  36. /*
  37.  * hsc_include
  38.  *
  39.  * read from inpf, parse for hsc-commands and execute them,
  40.  * check for html-error,
  41.  * write all out to outf and close input file.
  42.  *
  43.  * params: inpfnm...input file name
  44.  *         outf.....output file structure, already opended
  45.  *
  46.  * result: TRUE, if all worked well, else FALSE
  47.  */
  48. static BOOL hsc_include(HSCPRC * hp, INFILE * inpf, ULONG optn, INFILEPOS * base_pos)
  49. {
  50.     BOOL ok;                    /* result */
  51.     ok = (inpf != NULL);
  52.  
  53.     if (optn & IH_POS_PARENT)
  54.     {
  55.         panic("IH_POS_PARENT set");
  56.     }
  57.  
  58.     if (inpf)
  59.     {                           /* file opened? */
  60.         /* push current input file on input-file-stack */
  61.         if (hp->inpf)
  62.             ins_dlnode(hp->inpf_stack, hp->inpf_stack->first, (APTR) hp->inpf);
  63.  
  64.         /* set new base position for input-file */
  65.         /* (if called from a macro or after eg. <$source>) */
  66.         if (base_pos)
  67.             set_infile_base(inpf, base_pos);
  68.  
  69.         /* assign new input file to hsc-process */
  70.         hp->inpf = inpf;
  71.  
  72.         /* hide status? */
  73.         if ((optn & IH_PARSE_MACRO) || (optn & IH_PARSE_MACRO))
  74.             optn |= IH_NO_STATUS;
  75.  
  76.         /* set char-parse methods */
  77.         inpf->is_nc = hsc_normch;       /* set is_nc-methode */
  78.         inpf->is_ws = hsc_whtspc;       /* set is_ws-methode */
  79.  
  80.         /* status message: reading new file */
  81.         if (!(optn & IH_NO_STATUS) && !infeof(inpf))
  82.             hsc_status_file_begin(hp, infget_fname(hp->inpf));
  83.  
  84.         while (!infeof(inpf) && ok)
  85.         {                       /* parse file */
  86.             if (!(optn & IH_NO_STATUS) &&       /* status message */
  87.                 (hp->prev_status_line != infget_y(hp->inpf))
  88.                 )
  89.             {
  90.                 hsc_status_line(hp);
  91.                 hp->prev_status_line = infget_y(hp->inpf);
  92.             }
  93.             /* parse next item */
  94.             if (optn & IH_PARSE_SOURCE)
  95.                 ok = hsc_parse_source(hp);
  96.             else
  97.                 ok = hsc_parse(hp);
  98.         }
  99.  
  100.         /* parse at end: check for missing tags, .. */
  101.         if (ok && (optn & IH_PARSE_END))
  102.         {                       /* parse end (unclosed tags etc) */
  103.             ok = hsc_parse_end(hp);
  104.  
  105.             if (ok && (optn & IH_UPDATE_PRJ))
  106.                 ok = hsc_parse_end_id(hp);      /* update project file */
  107.         }
  108.  
  109.         /* end of file status */
  110.         if (!(optn & IH_NO_STATUS))
  111.         {
  112.             hsc_status_file_end(hp);    /* status message: file processed */
  113.         }
  114.         infclose(hp->inpf);     /*    close file */
  115.  
  116.         /* pull previous input file from input-file-stack
  117.          * or end hsc-process
  118.          */
  119.         if (hp->inpf_stack->first)
  120.         {
  121.             /* pull first item from stack */
  122.             hp->inpf = (INFILE *) hp->inpf_stack->first->data;
  123.             hp->inpf_stack->first->data = NULL;
  124.  
  125.             del_dlnode(hp->inpf_stack, hp->inpf_stack->first);
  126.         }
  127.         else
  128.         {
  129.  
  130.             hp->inpf = NULL;
  131.         }
  132.  
  133.     }
  134.     else
  135.         panic("no input file");
  136.  
  137.     return (ok);
  138. }
  139.  
  140. /*
  141.  * hsc_include_file
  142.  *
  143.  * open input file and include it
  144.  */
  145. BOOL hsc_base_include_file(HSCPRC * hp, STRPTR filename, ULONG optn, INFILEPOS * base_pos)
  146. {
  147.     BOOL ok = FALSE;
  148.     INFILE *inpf = NULL;
  149.  
  150.     /* status message: reading input */
  151.     if (!(optn & (IH_PARSE_MACRO | IH_PARSE_HSC)))
  152.     {
  153.         hsc_status_file_begin(hp, filename);
  154.     }
  155.  
  156.     /* check for stdin to use as input-file */
  157.     if ( !strcmp(filename, FILENAME_STDIN))
  158.         filename = NULL;
  159.  
  160.     /* open & read input file */
  161.     errno = 0;
  162.     inpf = infopen(filename, ES_STEP_INFILE);
  163.  
  164.     if (inpf)
  165.     {
  166.         /* include opened file */
  167.         ok = hsc_include(hp, inpf, optn, base_pos);
  168.  
  169.         /* check if this file is the main-source-file
  170.          * or an include-file and update project-data
  171.          * if neccessary
  172.          */
  173.         if (ok && hp->project)
  174.         {
  175.             if (optn & IH_IS_SOURCE)
  176.             {
  177.                 if (!filename)
  178.                     filename = FILENAME_STDIN;
  179.                 D(fprintf(stderr, DHL "INCLUDE source: `%s'\n", filename));
  180.                 hsc_project_set_source(hp->project, filename);
  181.                 /*reallocstr(&(hp->document->sourcename), filename); */
  182.             }
  183.  
  184.             /* check if this file is an include-file
  185.              * and update project-data if neccessary */
  186.             if (filename && (optn & IH_IS_INCLUDE))
  187.             {
  188.                 D(fprintf(stderr, DHL "INCLUDE subfile: `%s'\n", filename));
  189. #if 1
  190.                 hsc_project_add_include(hp->project, filename);
  191. #else /* TODO: remove */
  192.                 HSCINC *inc = app_include(hp->document, filename);
  193.                 INFILEPOS *fpos = NULL;
  194.                 if (hp->inpf)
  195.                     fpos = new_infilepos(hp->inpf);
  196.                 inc->caller = fpos2caller(fpos);
  197.                 if (fpos)
  198.                     del_infilepos(fpos);
  199. #endif
  200.             }
  201.         }
  202.     }
  203.     else
  204.     {
  205.         hsc_msg_noinput(hp, filename);  /* couldn't open file */
  206.     }
  207.  
  208.     return (ok);
  209. }
  210.  
  211. /*
  212.  * hsc_include_string
  213.  *
  214.  * open string as input file and include it
  215.  */
  216. BOOL hsc_base_include_string(HSCPRC * hp, STRPTR filename, STRPTR s, ULONG optn, INFILEPOS * base_pos)
  217. {
  218.     BOOL ok;
  219.     INFILE *inpf = NULL;
  220.  
  221.     if (optn & IH_POS_PARENT)
  222.     {
  223.         filename = PARENT_FILE_ID;
  224.         optn &= ~IH_POS_PARENT;
  225.     }
  226.     inpf = infopen_str(filename, s, 0);         /* try to open input file */
  227.  
  228.     ok = hsc_include(hp, inpf, optn, base_pos);
  229.  
  230.     return (ok);
  231. }
  232.  
  233. /* hsc_include_file: include file without base-position */
  234. BOOL hsc_include_file(HSCPRC * hp, STRPTR filename, ULONG optn)
  235. {
  236.     return (hsc_base_include_file(hp, filename, optn, NULL));
  237. }
  238.  
  239. /* hsc_include_string: include string without base-position */
  240. BOOL hsc_include_string(HSCPRC * hp, STRPTR filename, STRPTR s, ULONG optn)
  241. {
  242.     return (hsc_base_include_string(hp, filename, s, optn, NULL));
  243. }
  244.  
  245.